package main

import (
	"time"
)


var TimeOut = [5]time.Duration{9999 * time.Second, 1100 * time.Millisecond, 780 * time.Millisecond, 550 * time.Millisecond, 300 * time.Millisecond}
var DifficultyLevel = 0
var DifficultyChannel =make(chan int)

var ActionChannel = make(chan int)

func ScanSingleKey(signal chan<- int,snake *Snake) {
	resetSignal:=make(chan struct{})

	go func() {
		timer := time.NewTimer(TimeOut[DifficultyLevel])

		for true {
			select {
			case <-timer.C:
				signal <- NoChange
				/*select {
				case newDifficulty:=<-DifficultyChannel:
					DifficultyLevel=newDifficulty
				default:
				}*/
				timer.Reset(TimeOut[DifficultyLevel])
				//ok
			case <-resetSignal:
				timer.Reset(TimeOut[DifficultyLevel])
			case newDifficulty := <-DifficultyChannel:
				DifficultyLevel=newDifficulty
				timer.Reset(TimeOut[DifficultyLevel])
				//ok
			}
		}
	} ()

	for {
		keyAsInt:=<-ActionChannel

		//awsd
		if (keyAsInt == A) && (snake.SnakeToward != Right) && (snake.SnakeToward != Left) {
			resetSignal<- struct{}{}
			signal <- Left
		}
		if (keyAsInt == D) && (snake.SnakeToward != Right) && (snake.SnakeToward != Left) {
			resetSignal<- struct{}{}
			signal <- Right
		}
		if (keyAsInt == W) && (snake.SnakeToward != Up) && (snake.SnakeToward != Down) {
			resetSignal<- struct{}{}
			signal <- Up
		}
		if (keyAsInt == S) && (snake.SnakeToward != Up) && (snake.SnakeToward != Down) {
			resetSignal<- struct{}{}
			signal <- Down
		}
	}
}